package com.oa.service.user.impl;

import com.alibaba.fastjson.JSON;
import com.oa.config.QiniuConfigProperties;
import com.oa.mapper.user.UserMapper;
import com.oa.service.user.FileUploadService;
import com.oa.vo.FileUploadVo;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;
import com.qiniu.util.StringMap;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.UUID;

/**
 * @Author: 一拳超人
 * @Date: 2021/10/10 15:40
 */

@Service
@Slf4j
@EnableConfigurationProperties(QiniuConfigProperties.class)
@PropertySource("classpath:image.properties")
public class FileUploadImpl implements FileUploadService, InitializingBean {
    @Autowired
    private QiniuConfigProperties qiniuConfigProperties;

    @Autowired
    private UploadManager uploadManager;

    @Autowired
    private BucketManager bucketManager;

    @Autowired
    private Auth auth;

    private StringMap putPolicy;

    @Override
    public FileUploadVo uploadFile(InputStream inputStream) throws QiniuException {
        Response response = this.uploadManager.put(inputStream, null, getUploadToken(), null, null);
        int retry = 0;
        while (response.needRetry() && retry < 3) {
            response = this.uploadManager.put(inputStream, null, getUploadToken(), null, null);
            retry++;
        }
        //解析结果
        DefaultPutRet putRet = JSON.parseObject(response.bodyString(), DefaultPutRet.class);
        if (response.statusCode == 200) {
            String return_path = "https://" + qiniuConfigProperties.getPrefix() + "/" + putRet.key;
            log.info("文件名称={}", return_path);
            return new FileUploadVo(return_path, putRet.key);
        }
        return new FileUploadVo("201", "失败");
    }

    @Override
    public Integer deleteUploadFile(String key) throws QiniuException {
        Response response = bucketManager.delete(qiniuConfigProperties.getBucket(), key);
        int retry = 0;
        while (response.needRetry() && retry++ < 3) {
            response = bucketManager.delete(qiniuConfigProperties.getBucket(), key);
        }
        return response.statusCode;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        this.putPolicy = new StringMap();
        putPolicy.put("returnBody", "{\"key\":\"$(key)\",\"hash\":\"$(etag)\",\"bucket\":\"$(bucket)\",\"width\":$(imageInfo.width), \"height\":${imageInfo.height}}");
        // 自定义文件名字
        putPolicy.put("saveKey", UUID.randomUUID().toString().replace("-", ""));
    }

    /**
     * 获取上传凭证
     *
     * @return 上传凭证
     */
    private String getUploadToken() {
        return this.auth.uploadToken(qiniuConfigProperties.getBucket(), null, 3600, putPolicy);
    }
}
